package com.bhs.sansong.ui.findLogistics;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.DedicatedLineAdapter;
import com.bhs.sansong.base.BaseFragment;
import com.bhs.sansong.bean.DedicatedLineBean;
import com.bhs.sansong.bean.DedicatedLineDataBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.MyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 专线信息
 */
public class PrivateLineInformationFragment extends BaseFragment implements NetCallBack {


    private RecyclerView mRvList;
    private DedicatedLineAdapter mAdapter;
    private int page = 1;
    private int logistics_id;

    public static PrivateLineInformationFragment newInstance(int logistics_id) {
        PrivateLineInformationFragment fragment = new PrivateLineInformationFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("logistics_id", logistics_id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_private_line_information;
    }

    @Override
    public void initView(View root) {
        logistics_id = getArguments().getInt("logistics_id");
        mRvList = root.findViewById(R.id.rv_list);
    }

    @Override
    public void initData() {
        mAdapter = new DedicatedLineAdapter();
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        loadingPopupShow();
        loadData();
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                DedicatedLineBean info = mAdapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.btn_range_transport:
                        startActivity(new Intent(getActivity(), RangeTransportActivity.class)
                                .putExtra("end_city", info.getEnd_city())
                                .putExtra("end_county", info.getEnd_county())
                                .putExtra("end_province", info.getEnd_province())
                        .putExtra("logistics_id", info.getLogistics_id()));
                        break;
                    case R.id.iv_status:
                    case R.id.tv_line_name:
                        info.setChecked(!info.isChecked());
                        mAdapter.notifyItemChanged(position);
                        break;
                    case R.id.fl_phone_number:
                        MyUtils.callPhone(getContext(), info.getLine_mobile());
                        break;
                    case R.id.fl_shipping_phone:
                        MyUtils.callPhone(getContext(), info.getOutlets_list().get(0).getOutlets_mobile());
                        break;
                }

            }
        });

    }

    private void loadData() {
        networkRequest(netApi.getSpecialList(page, 10, logistics_id), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        loadingPopupDismiss();
        DedicatedLineDataBean bean = new Gson().fromJson(json, DedicatedLineDataBean.class);
        if (bean.isStatus()) {
            bean.getData().getList().get(0).setChecked(true);
            mAdapter.setNewData(bean.getData().getList());
        }
    }

    @Override
    public void onFailure(int requestCode) {
        loadingPopupDismiss();
    }
}